package com.example.pinkcat.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.AboutPinkCat;
import com.example.pinkcat.activity.FansFollowPage;
import com.example.pinkcat.activity.LoginRegisterPage;
import com.example.pinkcat.activity.PinCatAgreement;
import com.example.pinkcat.activity.PinkCatVip;
import com.example.pinkcat.activity.SuggestPage;
import com.example.pinkcat.activity.UserInfoChangePage;
import com.example.pinkcat.activity.Userpage;
import com.example.pinkcat.activity.pinkCatUser.Save_set_Msg;
import com.example.pinkcat.activity.pinkCatUser.UserMsg;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.example.pinkcat.api.entity.FanKeepStaticMsg;
import com.example.pinkcat.api.getData.getData_waibu;
import com.rishabhharit.roundedimageview.RoundedImageView;

public class Fragment_wode extends Fragment {
    //声明
    public View user_xy;
    public View user_ys;
    public View aboutus;
    public View zhzl;
    public View user_fk;

    //头像
    public View wode_userpage;
    public RoundedImageView wode_tx;
    public TextView username;
    TextView isvip;

    public View wode_btn1;
    public View wode_btn2;
    public View wode_btn3;

    public TextView login;

    View btn_vip;//vip按钮

    public TextView wode_btn1_dt_num;
    public TextView wode_btn2_keep_num;
    public TextView wode_btn3_fan_num;

    Context context;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wode,container,false);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        //登陆按钮初始化
        if (userStaticMsg.head_img!=null)
            if (!userStaticMsg.head_img.equals("null"))
                Glide.with(context).load(Uri.parse(userStaticMsg.head_img)).into(wode_tx);
        username.setText(userStaticMsg.userName);
        isvip.setText("你目前不是大会员");
        if (userStaticMsg.vip!=0){//如果是vip
            username.setTextColor(getResources().getColor(R.color.pink));
            isvip.setText("你是大会员");
        }
        if (userStaticMsg.login){
            login.setText("退出");
            login.setTextColor(getResources().getColor(R.color.black_overlay));
            login.setBackground(getResources().getDrawable(R.drawable.backgroud_cycle_white));

            getData_waibu.getkeep(context,userStaticMsg.userId);
            wode_btn1_dt_num.setText(String.valueOf(FanKeepStaticMsg.moving));
            wode_btn2_keep_num.setText(String.valueOf(FanKeepStaticMsg.keep));
            wode_btn3_fan_num.setText(String.valueOf(FanKeepStaticMsg.fan));


            if (!userStaticMsg.head_img.equals("null"))
                Glide.with(context).load(Uri.parse(userStaticMsg.head_img)).into(wode_tx);
                username.setText(userStaticMsg.userName);
            if (userStaticMsg.vip!=0){
                username.setTextColor(getResources().getColor(R.color.pink));
            }
        }
        else {
            login.setText("登陆");
            login.setTextColor(getResources().getColor(R.color.white));
            login.setBackground(getResources().getDrawable(R.drawable.backgroud_cycle_pink));
        }

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        context=view.getContext();

        login = view.findViewById(R.id.wode_login);
        wode_tx  = view.findViewById(R.id.wode_img_1);
        username = view.findViewById(R.id.wode_name);
        btn_vip=view.findViewById(R.id.wode_vip);


        wode_btn1_dt_num = view.findViewById(R.id.wode_tv_dongtai_num);
        wode_btn2_keep_num = view.findViewById(R.id.wode_tv_guanzhu_num);
        wode_btn3_fan_num = view.findViewById(R.id.wode_tv_fensi_num);
        isvip=view.findViewById(R.id.isvip);

        //跳转vip
        btn_vip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), PinkCatVip.class);
                view.getContext().startActivity(intent);
            }

        });

        //登陆按钮初始化
        if (userStaticMsg.login){
            login.setText("退出");
            login.setTextColor(getResources().getColor(R.color.black_overlay));
            login.setBackground(getResources().getDrawable(R.drawable.backgroud_cycle_white));
        }
        else {
            login.setText("登陆");
            login.setTextColor(getResources().getColor(R.color.white));
            login.setBackground(getResources().getDrawable(R.drawable.backgroud_cycle_pink));
        }

        user_xy = view.findViewById(R.id.wode_rl_3);
        user_xy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PinCatAgreement.class);
                intent.putExtra("agreementTitle","用户协议");
                startActivity(intent);
            }
        });

        user_ys = view.findViewById(R.id.wode_rl_4);
        user_ys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PinCatAgreement.class);
                intent.putExtra("agreementTitle","隐私协议");
                startActivity(intent);
            }
        });

        aboutus = view.findViewById(R.id.wode_rl_2);
        aboutus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), AboutPinkCat.class);
                startActivity(intent);
            }
        });

        zhzl = view.findViewById(R.id.wode_zhzl);
        zhzl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), UserInfoChangePage.class);
                startActivity(intent);
            }
        });

        user_fk = view.findViewById(R.id.wode_rl_1);
        user_fk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), SuggestPage.class);
                startActivity(intent);
            }
        });


        //头像

        wode_userpage = view.findViewById(R.id.wode_main_body_1);
        wode_userpage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userStaticMsg.login){
                    Intent intent = new Intent(getContext(),Userpage.class);
                    intent.putExtra("up_id",userStaticMsg.userId);
                    startActivity(intent);
                }else {
                    Toast.makeText(view.getContext(),"当前未登陆,请登录",Toast.LENGTH_SHORT).show();
                }

            }
        });

        //动态
        wode_btn1 = view.findViewById(R.id.wode_bar_1_btn);
        wode_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userStaticMsg.userId.equals("-1"))
                    return;
                Intent intent = new Intent(getContext(), Userpage.class);
                intent.putExtra("isCollectPage",false);
                intent.putExtra("up_id",userStaticMsg.userId);
                startActivity(intent);
            }
        });

        //关注
        wode_btn2 = view.findViewById(R.id.wode_bar_2_btn);
        wode_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), FansFollowPage.class);
                intent.putExtra("isFansPage",false);
                intent.putExtra("up_id",userStaticMsg.userId);
                startActivity(intent);
            }
        });

        //粉丝
        wode_btn3 = view.findViewById(R.id.wode_bar_3_btn);
        wode_btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), FansFollowPage.class);
                intent.putExtra("isFansPage",true);
                intent.putExtra("up_id",userStaticMsg.userId);
                startActivity(intent);
            }
        });

        //登陆
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userStaticMsg.login){
                    userStaticMsg.userId= "-1";//-1
                    userStaticMsg.userName="pink_cat";
                    userStaticMsg.phone="null";
                    userStaticMsg.mail="null";
                    userStaticMsg.head_img = "null";
                    userStaticMsg.login=false;
                    userStaticMsg.vip=0;
                    userStaticMsg.style="phonecode";
                    userStaticMsg.appVersion=null;
                    userStaticMsg.appUrl=null;
                    userStaticMsg.JSESSIONID=null;
                    userStaticMsg.password=null;
                    login.setText("登陆");
                    login.setBackground(getResources().getDrawable(R.drawable.backgroud_cycle_pink));
                    Save_set_Msg.Save(new UserMsg(),context);
                    onStart();
                }else {
                    Intent intent = new Intent(getContext(), LoginRegisterPage.class);
                    startActivity(intent);
                }

            }
        });
    }
}